// pages/enterpriseCertification/enterpriseCertification.js
const app = getApp()
const cfg = require("../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标  1表示显示  0表示不显示
      title: '企业认证', //导航栏 中间的标题
      white: true, // 是就显示白的，不是就显示黑的。
      address: '' // 加个背景 不加就是没有
    },
    height: app.globalData.height * 2 + 30,
    checkIcon: '../../images/Check3x.png',
    showModalStatus: false,
    tempFileUp: '',
    tempFileUp_:'',
    tempFileDwon: '',
    bankName: '',
    agentBankAccount: '',
    bankNameCode:'',
    sellerName:'',
    sellerPhone:'',
    taxPayerCode:'',
    bankAddress:'',
    bankCode: '',
    bankNameSubject: '',
    agentBankName: '',
    bankList: [],
    checkTrue: "",
    isAuthentication:'', //没数据-未认证，Y-已认证，N-拒绝认证，X-审核中
    businessLicenseFile: "",
    d_url:cfg.requestURL,
    isReadOnly: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBankList();
    this.getAuthInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      bankNameSubject: wx.getStorageSync('sellerCompanyName'),
      isAuthentication: wx.getStorageSync('isAuthentication')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeBankCode:function(e){
    this.setData({
      bankNameCode: e.detail.value
    })
  },
  changeBankName: function (e) {
    this.setData({
      bankName: e.detail.value
    })
  },
  changeAgentBankName: function (e) {
    this.setData({
      agentBankName: e.detail.value
    })
  },
  changeBankSubject: function (e) {
    console.log(e.detail.value)
    wx.setStorageSync('sellerCompanyName', e.detail.value)
    this.setData({
      bankNameSubject: e.detail.value
    })
  },
  changeBankAccount: function (e) {
    console.log(e)
    this.setData({
      agentBankAccount: e.detail.value
    })
  },
  changeAddress:function(e){
    this.setData({
      bankAddress: e.detail.value
    })
  },
  changeSellName:function(e){
    this.setData({
      sellerName: e.detail.value
    })
  },
  changesellerPhone: function (e) {
    this.setData({
      sellerPhone: e.detail.value
    })
  },
  uploadUp: function () {
    if (this.data.isReadOnly) {
      return;
    }
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 1000
        })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFileUp = res.tempFilePaths;
        that.setData({
          tempFileUp_: tempFileUp
        })
        wx.uploadFile({
          url: cfg.requestURL + '/backend/upload/mobile/upload?token=' + wx.getStorageSync('token'), //开发者服务器的 url
          filePath: tempFileUp[0], // 要上传文件资源的路径 String类型！！！
          name: 'file', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
          header: {
            'content-type': 'multipart/form-data'
          }, // 设置请求的 header
          formData: {}, // HTTP 请求中其他额外的参数
          success: function (res) {
            that.setData({
              tempFileUp: JSON.parse(res.data).data
            })
            console.log("JSON.parse(res.data).data=" + JSON.parse(res.data).data);
          },
          fail: function (res) {
          }
        })
      }
    })
  },
  getBankList: function () {
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/agent/mobile/cash/bankcodelist', //仅为示例，并非真实的接口地址
      data: {
        "agentId": wx.getStorageSync('agentId'),
        "token": wx.getStorageSync('token'),
      },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          _this.setData({
            bankList: res.data.data,
            bankCode: res.data.data[0].bankCode,
            bankName: res.data.data[0].bankName
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  getAuthInfo: function() {
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/seller/mobile/sellerAuth?token=' + wx.getStorageSync('token'), //仅为示例，并非真实的接口地址
      data: {
        "sellerId": wx.getStorageSync('sellerId')
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        var data = res.data.data;
        if (data.isAuthentication == "Y" || data.isAuthentication == "X") {
          _this.setData({
            isReadOnly: "true",
            bankNameSubject: data.sellerCompanyName,
            sellerName: data.sellerName,
            sellerPhone: data.sellerPhone,
            taxPayerCode: data.taxPayerCode,
            bankNameCode: data.creditCode,
            bankAddress: data.sellerAddress,
            bankName: data.sellerBank,
            agentBankAccount: data.sellerBankAccount,
            isAuthentication: data.isAuthentication,
            businessLicenseFile:data.businessLicenseFile
          })
        } else {
          _this.setData({
            isReadOnly: ""
          })
        }
      }
    })
  },
  addBank: function () {
    var _this = this;
    console.log(_this.data.tempFileUp)
    wx.showLoading({
      title: '加载中...',
    })
    if (_this.data.bankNameSubject == "") {
      wx.showToast({
        title: '请输入公司名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.sellerName == "") {
      wx.showToast({
        title: '请输入法定代表人',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!(/^1[345789]\d{9}$/.test(_this.data.sellerPhone))) {
      wx.showToast({
        title: '手机号码填写有误',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (_this.data.bankNameCode == "") {
      wx.showToast({
        title: '请输入信用代码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.bankNameSubject == "") {
      wx.showToast({
        title: '请输入支行名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.tempFileUp_ == "") {
      wx.showToast({
        title: '请上传执照',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.bankAddress == "") {
      wx.showToast({
        title: '请输入办公地址',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.bankName == "") {
      wx.showToast({
        title: '请选择开户行',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (_this.data.agentBankAccount == "") {
      wx.showToast({
        title: '请输入银行卡号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: cfg.requestURL + '/backend/seller/mobile/sellerAuth?token=' + wx.getStorageSync('token'), //仅为示例，并非真实的接口地址
      data: {
        "sellerId": wx.getStorageSync('sellerId'),
        "sellerName": _this.data.sellerName,
        "sellerCompanyName": _this.data.bankNameSubject,
        "creditCode": _this.data.bankNameCode,
        "businessLicenseFile": _this.data.tempFileUp,
        "isAuthentication":'X',
        "sellerAddress": _this.data.bankAddress,
        "sellerPhone": _this.data.sellerPhone,
        "sellerBank": _this.data.bankName,
        "sellerBankAccount": _this.data.agentBankAccount
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          _this.setData({
            isAuthentication:'X',
            isReadOnly: "true"
          })
          wx.setStorageSync('isAuthentication',"X")
          wx.showToast({
            title: "认证申请已提交",
            icon: 'success',
            mask: true,
            duration: 1500
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  //显示对话框
  showModal: function () {
    if (this.data.isReadOnly) {
      return;
    }
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  changeBank: function (event) {
    let postId = event.currentTarget.dataset.id;
    let name = event.currentTarget.dataset.name;
    this.setData({
      bankCode: postId,
      bankName: name
    })
    this.hideModal();
  }
})